package com.itzdm.reggie.controller;

import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.entity.User;
import com.itzdm.reggie.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 发送验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    @ApiOperation("发送QQ邮箱验证码")
    public Result<String> sendMsg(@RequestBody User user, HttpSession session){
        log.info("发送验证码：{}",user);
        userService.sendMsg(user,session);
        return Result.success("发送成功");
    }

    /**
     * 用户登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<User> login(@RequestBody Map map, HttpSession session){
        log.info("用户登录:{}",map.toString());
        User user = userService.login(map, session);
        return Result.success(user);
    }

    @PostMapping("/loginout")
    public Result logout(HttpSession session){
        session.removeAttribute("user");
        return Result.success("退出登录成功");
    }
}
